//切换样式渲染
fanhuizhuye()
qieHuan()
getUpHead()

hisMy()

/**
 * 获取我的历史博客
 */
function hisMy() {
    myHistory_My(0, 50)
}


/**
 * 我的历史博客且分页渲染
 * @param index
 * @param yenum
 */
function myHistory_My(index, yenum) {
    future.ajax({
        url: '/article/myHistory',
        type: 'get',
        data: {
            num: index,
            yenum
        },
        success: function (result) {
            add_myHistory_My(result)

        }
    })
}

/**
 * 我的博客渲染
 * @param result
 */
function add_myHistory_My(result) {
    let my_HisDOM = document.querySelector('#My_hisList')
    let My_his = ``;
    result.forEach(function (item, index) {
        item.time = rTime(item.time)
        /*有的文章并没有图片*/
        if (item.thumbnail) {
            item.thumbnail = JsonstrToArrs(item.thumbnail)
            My_his += ` <li  onclick="goDetails(this)"  article_id=` + item.id + `>
                    <div class="CenRight1">
                        <div class="img_div"><img src="` + item.thumbnail[0] + `" alt=""></div>`
        } else {
            My_his += `
              <li>
                    <div class="CenRight1">
                        <div class="img_div"><img src="http://localhost:3000/article/2020080213421464823.jpg" alt=""></div>
            `
        }
        My_his += `
                        <div class="wenzi_div">
                            <span>` + item.title + `</span>
                            <i>` + item.time + `</i>
                          <div class="p_div"><p>` + item.contentText + `</p></div>

                        </div>

                    </div>
                </li>
        `
    })
    my_HisDOM.innerHTML = My_his;

}


/**
 * 返回主页
 */
function fanhuizhuye() {
    document.querySelector('#fanhui').onclick = function () {
      window.location.href = "../NewPage/html/New_index.html"
    }
}


/**
 *获取我的页面头部资料(头像,邮箱,名字渲染)
 */
function getUpHead() {
    future.ajax({
        url: '/users/getHead',
        type: 'post',
        success: function (result) {
            let imgUrl = "./file/" + result.head;
            let userName = result.username
            let mailbox = result.mailbox
            document.getElementById("My_img").src = imgUrl;
            document.getElementById("H1_title").innerText = userName + "的个人资料主页";
            document.getElementById("home_username").innerText = userName;
            document.getElementById("home_emil").innerText = mailbox;
            document.getElementById("personal_name").value = userName;
            document.getElementById("personal_emil").value = mailbox;
        }, error: function () {
            document.getElementById("My_img").src = " Homepage/Img/homeimg/touxiang1.jpg"
        }
    })
}

/**
 * 个人页面切换修改样式渲染
 * @constructor
 */
function qieHuan() {
    let upFalg = false;
    let up_btn = document.querySelector('#up_btn')
    let Center_sty = document.querySelector('#Center_sty')
    let Center_upUser = document.querySelector('#Center_upUser')
    let top2nav_ul = document.querySelector('#top2nav_ul')
    let hf_btn = my$("#huanfu_btn");
    let hf_nav = my$("#huanfu_nav")
    up_btn.onclick = function () {
        hf_btn.style.color = "white"
        hf_btn.style.background = "black"
        up_btn.style.color = "black"
        hf_nav.style.display = "none";
        up_btn.style.background = "white"
        if (upFalg) {
            upFalg = false;
            Center_sty.style.display = ""
            Center_upUser.style.display = "none"
            top2nav_ul.innerHTML = `  <li ><span >个人档案</span></li>
                    <li><span >历史博客</span></li>`
            up_btn.innerHTML = `<i   class="iconfont icon-ziliao"></i>修改页面`


        } else {
            upFalg = true;

            Center_sty.style.display = "none"
            Center_upUser.style.display = ""
            top2nav_ul.innerHTML = `<x>eg:邮箱绑定需要发送验证码到您的邮箱,通过您的邮箱确认才能更换</x>`
            up_btn.innerHTML = `<i   class="iconfont icon-ziliao"></i>展示页面`
        }
    }

    document.querySelector('#up_btn').onblur = function () {
        console.log("sasdasdas")
        up_btn.style.color = "black"
        up_btn.style.background = "white"
    }

}

/**
 * 转换时间格式
 * @param date
 * @returns {string}
 */
function rTime(date) {
    var json_date = new Date(date).toJSON();
    return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}

/**
 * json字符串转数组
 * @param str
 * @returns {any}
 * @constructor
 */
function JsonstrToArrs(str) {
    let arr = str.split(',');
    let s = JSON.parse(arr)
    return s;
}

/**
 * 绑定进入详情页函数
 * */
function goDetails(obj) {
    let article_id = obj.getAttribute("article_id");
    window.location.href = "../details.html?article_id=" + article_id

}